package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-10
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    //访问地址：http://localhost:8001/eduservice/teacher/findAll
    //Swagger 测试地址: http://localhost:8001/swagger-ui.html
    //service注入
    @Autowired
    private EduTeacherService teacherService;

    // 1. 查询表中所有数据
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")      //查询用get
    public R findAllTeacher(){
        //调用service方法查询mapper
        List<EduTeacher> teachers = teacherService.list(null);
        return R.ok().data("items",teachers);
    }

    //2. 讲师逻辑删除
    @ApiOperation(value = "根据ID删除讲师")   //注释，可加可不加
    @DeleteMapping("{id}")      //id值通过url传递给后台
    public R deleteTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)     //注释，可加可不加
            @PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //3. 分页查询
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable long current,
                         @PathVariable long limit){
        //创建page对象
        Page<EduTeacher> page = new Page<>(current,limit);
        //实现分页
        teacherService.page(page,null);
        long total = page.getTotal();   //总记录数
        List<EduTeacher> records = page.getRecords();    //每页数据的list集合

//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("rows",records);
//        return R.ok().data(map);

        return R.ok().data("total",total).data("rows",records);
    }

    //条件查询+分页+根据添加时间进行排序   如果传入的参数用RequestBody注解，则必须用PostMapping
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){    //required参数代表requestbody的值可以没有
        Page<EduTeacher> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询   Mybatis 动态sql 判断条件是否为空，如果不为空就拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);  //模糊查询
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);  //等于
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        //实现排序
        wrapper.orderByDesc("gmt_create");
        //实现分页查询
        teacherService.page(page,wrapper);
        long total = page.getTotal();   //总记录数
        List<EduTeacher> records = page.getRecords();    //每页数据的list集合
        return R.ok().data("total",total).data("rows",records);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if(save) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    //根据讲师id进行查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("teacher",teacher);
    }

    //修改讲师
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = teacherService.updateById(eduTeacher);
        if(update){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

